<template>
  <view>
    <!-- 使用子组件 -->
    <my-search-bar />

    <!-- 轮播图 -->
    <swiper indicator-dots autoplay circular :interval="3000">
      <navigator
        v-for="item in swipers"
        :key="item.goods_id"
        :url="'/subpkg/goods-detail/index?goods_id=' + item.goods_id"
      >
        <swiper-item>
          <image :src="item.image_src" mode="scaleToFill" />
        </swiper-item>
      </navigator>
    </swiper>

    <!-- 导航菜单 -->
    <view class="nav-list">
      <image
        @click="switchTab(item.name)"
        v-for="item in catitems"
        :key="item.name"
        :src="item.image_src"
        class="nav-img"
        mode="scaleToFill"
      />
    </view>

    <!-- 楼层数据 -->
    <view v-for="item in floorData" :key="item.floor_title.name">
      <!-- 头部部分 -->
      <view>
        <image
          class="floor-title"
          :src="item.floor_title.image_src"
          mode="scaleToFill"
        />
      </view>
      <!-- 内容 -->
      <view class="floor-img-box">
        <!-- 左边 -->
        <navigator :url="item.product_list[0].navigator_url">
          <image
            :src="item.product_list[0].image_src"
            :style="{ width: item.product_list[0].image_width + 'rpx' }"
            mode="widthFix"
          />
        </navigator>
        <!-- 右边 -->
        <view class="right-img-box">
          <navigator
            v-for="(subitem, index) in item.product_list"
            :key="subitem.name"
            :url="subitem.navigator_url"
            open-type="navigate"
            hover-class="navigator-hover"
          >
            <image
              v-show="index > 0"
              :src="subitem.image_src"
              :style="{ width: subitem.image_width + 'rpx' }"
              mode="widthFix"
            />
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import tabBarBadge from '@/mixins/tabBar-badge'
import { getSwipersDataApi, getCatitemsDataApi, getFloorDataApi } from '@/api/index'
import mySearchBar from '../../conponents/my-search-bar/my-search-bar.vue';
export default {
  components: { mySearchBar },
  mixins: [tabBarBadge],
  data () {
    return {
      swipers: [],
      catitems: [],
      floorData: []
    };
  },

  onLoad () {
    this.getSwipersData()
    this.getCatitemsData()
    this.getFloorData()
  },

  methods: {
    async getSwipersData () {
      const { meta: { status }, message } = await getSwipersDataApi()
      if (status === 200) {
        this.swipers = message
      }
    },

    async getCatitemsData () {
      const { meta: { status }, message } = await getCatitemsDataApi()
      if (status === 200) {
        this.catitems = message
      }
    },

    switchTab (name) {
      if (name !== '分类') return
      uni.switchTab({
        url: '/pages/category/index'
      })
    },

    async getFloorData () {
      const { meta: { status }, message } = await getFloorDataApi()
      if (status === 200) {
        message.forEach(item => {
          item.product_list.forEach(subitem => {
            subitem.navigator_url = '/subpkg/goods-list/index?' + subitem.navigator_url.split('?')[1]
          })
        })

        this.floorData = message
      }
    }


  }
}
</script>

<style lang="scss" scoped>
.search-box {
  // 设置定位效果为“吸顶”
  position: sticky;
  // 吸顶的“位置”
  top: 0;
  // 提高层级，防止被轮播图覆盖
  z-index: 999;
}

swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
